package de.breitbeil.jbooking.ui.dialog;

import org.springframework.richclient.dialog.FormBackedDialogPage;
import org.springframework.richclient.dialog.TitledPageApplicationDialog;
import org.springframework.richclient.form.Form;
import org.springframework.richclient.settings.SettingsException;
import org.springframework.richclient.settings.SettingsManager;

import de.breitbeil.jbooking.task.BookingTemplateTask;
import de.breitbeil.jbooking.ui.form.BookingTemplateForm;
import de.breitbeil.jbooking.ui.form.BookingTemplateFormObject;

public class BookingTemplateDialog extends TitledPageApplicationDialog {

    public static final String ID = "bookingTemplateDialog"; 

    private Form form;

    public BookingTemplateDialog(SettingsManager settingsManager) throws SettingsException {
        BookingTemplateFormObject formObject = new BookingTemplateFormObject();
        formObject.init(settingsManager.getUserSettings());
        
        form = new BookingTemplateForm(formObject);
        
        setDialogPage(new FormBackedDialogPage(form));
    }
    
    public BookingTemplateDialog(BookingTemplateFormObject formObject) throws SettingsException {
        form = new BookingTemplateForm(formObject);
        setDialogPage(new FormBackedDialogPage(form));
    }

    protected void onInitialized() {
        super.onInitialized();

        setTitle(getMessage(ID + ".title"));
        setTitlePaneTitle(getMessage(ID + ".title"));
        setDescription(getMessage(ID + ".description"));

        setResizable(false);
    }
    
    @Override
    protected boolean onFinish() {
        form.commit();
        BookingTemplateFormObject formObject = (BookingTemplateFormObject) form.getFormObject();
        
        // Save calendar to file
        BookingTemplateTask task = new BookingTemplateTask(formObject);
        task.execute();
        
        return true;
    }

}
